package com.hellobike.haxing.modules.order.listener;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hellobike.haxing.common.constant.CacheConstant;
import com.hellobike.haxing.common.util.RedisUtil;
import com.hellobike.haxing.modules.order.constant.OrderConstants;
import com.hellobike.haxing.modules.order.entity.Order;
import com.hellobike.haxing.modules.order.entity.OrderGroup;
import com.hellobike.haxing.modules.order.mapper.OrderGroupMapper;
import com.hellobike.haxing.modules.order.mapper.OrderMapper;
import com.hellobike.haxing.modules.order.service.IOrderService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.CacheKey;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 支付成功订单
 */
@RocketMQMessageListener(topic = "success-order-topic", consumerGroup = "backendSuccessOrderListener")
@Component
@Data
@Slf4j
public class OrderSuccessListener implements RocketMQListener<Long> {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IOrderService orderService;

    @Override
    public void onMessage(Long id) {
        Object redisOrderId = redisUtil.get(CacheConstant.ORADER_SUCCESS_MQ_ORDERID+id);
        if(redisOrderId!=null){
            log.error("=支付成功订单同步采购单MQ重复消费==订单ID={}==",id);
            return;
        }
        orderService.updateSyncPurchase(id,1);
        redisUtil.set(CacheConstant.ORADER_SUCCESS_MQ_ORDERID+id,id,CacheConstant.ONE_DAY);
        log.info("=支付成功订单同步采购单MQ消费成功==订单ID={}==",id);
    }
}
